<?php
/**
 * 促销页面
 *
 */
defined('InShopNC') or exit('Access Invalid!');
class seckillControl extends MobileControl{
    public function indexOp() {
		//分开写
		$time = time();
		$day_time = strtotime(date("Y-m-d"));
		$fields = 'goods_id , goods_name,miaosha_endtime,miaosha_starttime,miaosha_flag,goods_image,miaosha_price';
		//正在秒杀
		$sql = "SELECT {$fields} FROM #__goods WHERE group_flag='0'AND xianshi_flag='0' AND miaosha_flag='1' AND goods_show='1'AND goods_state='0'  AND miaosha_starttime <{$time} AND miaosha_endtime >{$time}";
		$sql .= " ORDER BY miaosha_starttime asc";
		$jinxing_list = db::queryAll($sql);
		$member_id = isset($_SESSION['member_id']) ? $_SESSION['member_id'] : 0;
		foreach ($jinxing_list as $k => $v) {
			//剔除当天
			if ($member_id) {
				$order_goods_row = Db::queryRow("SELECT a.order_id,a.goods_id FROM #__order_goods as a left join #__order as b on a.order_id=b.order_id where a.goods_id={$v['goods_id']} and b.buyer_id={$member_id} and a.is_miaosha=1 and add_time >" . $day_time);
				//判断是否当天已秒杀过
				if ($order_goods_row)
					unset($jinxing_list[$k]);
			}

		}

		//预告
		$sql = "SELECT {$fields} FROM #__goods WHERE group_flag='0'AND xianshi_flag='0' AND miaosha_flag='1' AND goods_show='1'AND goods_state='0'  AND miaosha_starttime >{$time} ";
		$sql .= " ORDER BY miaosha_starttime asc";
		$tmp_list = db::queryAll($sql);
		$yugao_list = array();
		foreach ($tmp_list as $k => $v) {
			$d = date("m-d", $v['miaosha_starttime']);
			$yugao_list[$d][] = $v;
		}
		//结束
		$sql = "SELECT {$fields} FROM #__goods WHERE group_flag='0'AND xianshi_flag='0' AND miaosha_flag='1' AND goods_show='1'AND goods_state='0'  AND miaosha_endtime <{$time} ";
		$sql .= " ORDER BY miaosha_starttime asc";
		$jieshu_list = db::queryAll($sql);
		$goods_list[1] = $jinxing_list;
		$goods_list[2] = $yugao_list;
		$goods_list[3] = $jieshu_list;
		unset($tmp_list, $jinxing_list, $yugao_list, $jieshu_list);
		Tpl::output('goods_list', $goods_list);
		Tpl::output('member_id', $member_id);
		Tpl::showpage('seckill');
	}
}